package cp.remote.controller;

import cp.remote.R;
import cp.remote.config.*;
import cp.remote.network.Network;
import cp.remote.network.TaskSwitchNetWorker;
import cp.remote.timer.ConfigTimer;
import android.app.Activity;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;

public class Config implements OnClickListener {

    private Activity conf = null;
    private EditText txt;
    private int checkkinect = View.GONE, checkadvance = View.GONE;

    public Config() {
        super();
    }

    public void setActivity(Activity conf) {
        this.conf = conf;
        txt = (EditText) conf.findViewById(R.id.conf_textarea_progress);
    }

    private void setConst() {
        setProgressTXT("Set Constants");
        NetworkConfig.ip = ((EditText) conf.findViewById(R.id.conf_txtIP))
                .getText().toString();
        NetworkConfig.remoteport = Integer.parseInt(((EditText) conf
                .findViewById(R.id.conf_txtRePort)).getText().toString());
        NetworkConfig.taskport = Integer.parseInt(((EditText) conf
                .findViewById(R.id.conf_txtTaPort)).getText().toString());
        NetworkConfig.kinectport = Integer.parseInt(((EditText) conf
                .findViewById(R.id.conf_txtKinectPort)).getText().toString());
        NetworkConfig.timeout = Integer.parseInt(((EditText) conf
                .findViewById(R.id.conf_txtAdvanceTimeout)).getText()
                .toString());
        KinectConfig.imgheight = Integer.parseInt(((EditText) conf
                .findViewById(R.id.conf_txtKinectImgHeight)).getText()
                .toString());
        KinectConfig.imgwidth = Integer.parseInt(((EditText) conf
                .findViewById(R.id.conf_txtKinectImgWidth)).getText()
                .toString());
        KinectConfig.kinectimage = ((CheckBox) conf
                .findViewById(R.id.cbKinectImage)).isChecked();
        setProgressTXT("Finish");
    }

    public void onClick(View v) {
        txt.setText("");
        switch (v.getId()) {
        case R.id.conf_cmbConnect:
            connect();
            break;
        case R.id.conf_cmbDisconnect:
            disconnect();
            break;
        case R.id.conf_cmbApply:
            apply();
            break;
        case R.id.cbKinectImage:
            kinectimage(v);
            break;
        case R.id.conf_cbAdvance:
            advance(v);
            break;
        }

    }

    private void disconnect() {
        if (NetworkConfig.nettask != null)
            NetworkConfig.nettask.stop();
        NetworkConfig.nettask = null;
        txt.setVisibility(View.GONE);
        conf.invalidateOptionsMenu(); // Refresh option menu
    }

    private void connect() {
        txt.setVisibility(View.VISIBLE);

        setProgressTXT("Try Connect");
        setProgressTXT("Starting Init Phase");
        setConst();
        setProgressTXT("Start TaskSwitch Networker");
        NetworkConfig.nettask = new TaskSwitchNetWorker();
        NetworkConfig.nettask.start();
        setProgressTXT("Finish");

        setProgressTXT("Finish Init Phase");

        setProgressTXT("Wait for Server Connections");
        setConnectionHandler(NetworkConfig.nettask);
    }

    private void apply() {
        txt.setVisibility(View.VISIBLE);
        String ip = getEditText(R.id.conf_txtIP);
        int tport = Integer.parseInt(getEditText(R.id.conf_txtTaPort));

        if (ip.equals(NetworkConfig.ip) && tport == NetworkConfig.taskport) {
            setConst();
        } else {
            disconnect();
            connect();
        }
    }

    private void kinectimage(View v) {
        checkkinect = getCheckBoxVisible(v);
        conf.findViewById(R.id.conf_txtKinectPort).setVisibility(checkkinect);
        conf.findViewById(R.id.textView4).setVisibility(checkkinect);
        setVisibleAdvanceKinect();
    }

    private void advance(View v) {
        checkadvance = getCheckBoxVisible(v);
        conf.findViewById(R.id.conf_labelAdvanceTimeout).setVisibility(
                checkadvance);
        conf.findViewById(R.id.conf_txtAdvanceTimeout).setVisibility(
                checkadvance);
        setVisibleAdvanceKinect();
    }

    private void setProgressTXT(String txt) {
        this.txt.append(txt + "\n");
    }

    private void setConnectionHandler(Network w) {
        Handler handler = new Handler();
        ConfigTimer t = new ConfigTimer(handler, conf, w);
        handler.removeCallbacks(t);
        handler.postDelayed(t, 50);
    }

    private int getCheckBoxVisible(View v) {
        CheckBox temp = (CheckBox) v;
        if (temp.isChecked()) {
            return View.VISIBLE;
        } else {
            return View.GONE;
        }
    }

    private String getEditText(int id) {
        return ((EditText) conf.findViewById(id)).getText().toString();
    }

    private void setVisibleAdvanceKinect() {
        if (checkadvance == View.VISIBLE) {
            conf.findViewById(R.id.conf_label_kinect_imgHeight).setVisibility(
                    checkkinect);
            conf.findViewById(R.id.conf_label_kinect_imgWidth).setVisibility(
                    checkkinect);
            conf.findViewById(R.id.conf_txtKinectImgHeight).setVisibility(
                    checkkinect);
            conf.findViewById(R.id.conf_txtKinectImgWidth).setVisibility(
                    checkkinect);
        }
    }
}
